using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ReplyDAL
    {
        public static int Save(ReplyInfo info)
        {
            string sql = string.Format("insert into [reply]([AdminID],[UserID],[SID],[Score],[Context],[TableName],[CreateDate]) values(@AdminID,@UserID,@SID,@Score,@Context,@TableName,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@Score", info.Score),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@TableName", info.TableName),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(ReplyInfo info)
        {
            string sql = string.Format("update [reply] set [AdminID]=@AdminID,[SID]=@SID,[Score]=@Score,[Context]=@Context,[TableName]=@TableName where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@Score", info.Score),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@TableName", info.TableName),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static ReplyInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[reply]");
            return SimpleData(dr);
        }
        public static List<ReplyInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[reply]");
            return ListSimpleData(dt);
        }
        public static List<ReplyInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [reply].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [reply].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[reply]", out count);
            return ListSimpleData(dt);
        }
        public static List<ReplyInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [reply]"));
            return ListSimpleData(dt);
        }
        private static ReplyInfo SimpleData(DataRow dr)
        {
            ReplyInfo info = new ReplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.Score = DataConvert.ToInt(dr["Score"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.TableName = DataConvert.ToString(dr["TableName"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static ReplyInfo AllData(DataRow dr)
        {
            ReplyInfo info = new ReplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.Score = DataConvert.ToInt(dr["Score"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.TableName = DataConvert.ToString(dr["TableName"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<ReplyInfo> ListSimpleData(DataTable dt)
        {
            List<ReplyInfo> list = new List<ReplyInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ReplyInfo> ListAllData(DataTable dt)
        {
            List<ReplyInfo> list = new List<ReplyInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

